package com.enjava;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.springframework.beans.factory.annotation.Autowired;

import libreria.Libro;

@FacesConverter("libroConverter")
public class LibroConverter implements Converter{
	


	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		Long idLibro = Long.parseLong(value);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		EntityManagerFactory em=(EntityManagerFactory) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "entityManagerFactory");
		return em.createEntityManager().find(Libro.class, idLibro);
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		Libro libro=(Libro) value;
		return libro.getId()+"";
	}

	
}
